package dmmm.components;

import java.io.*;

public class MicroMEM extends LogicalComponent {

	private String[] memory;

	public MicroMEM(int size) {
		super(1, 3, false);

		out[0].setIsInt();
		out[0].setNumOfLines(8);
		out[1].setIsInt();
		out[1].setNumOfLines(5);
		out[2].setIsInt();
		out[2].setNumOfLines(23);

		memory = new String[size];

		int counter = 0;
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(
					new FileReader("conf/dmmm/microkod.txt"));
			String line;
			while ((line = reader.readLine()) != null) {
				memory[counter++] = line;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	@Override
	public void func() {
		int n = in[0].getIntVal();
		String s = memory[n];
		out[0].setIntVal(Integer.parseInt(s.substring(7, 9), 16));
		int visi = Integer.parseInt(s.substring(0, 7), 16);
		out[1].setIntVal(visi & 0x1f);
		out[2].setIntVal(visi >> 5);

	}

	public String getAllOut() {
		return memory[in[0].getIntVal()];
	}

	public static void main(String[] args) {

		MicroMEM mm = new MicroMEM(230);

		mm.setInputPin(0, new Pin(5, 8));

		mm.func();

		System.out.println(Integer.toHexString(mm.getOut(0).getIntVal()));
	}
}
